package cn.gson.springboot.model.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.mapper.RegistrFromMapper;
import cn.gson.springboot.model.pojos.ClinicCard;
import cn.gson.springboot.model.pojos.RegistrFrom;
import cn.gson.springboot.model.pojos.RegistrType;
import cn.gson.springboot.model.pojos.Section;
@Service
@Transactional(rollbackFor=Exception.class)
public class RegistrFromService {
	  //挂号mapper
	  @Autowired
	  private RegistrFromMapper registrmapper;
	  
	  //挂号类型service
	  @Autowired
	  private  RegistrTypeservice regts;
	  
	  //医疗卡的service	
	 @Autowired
	 private ClinicCardService  ccs;
	 
	 //交易记录service
	 @Autowired
	 private RechangeService res;
	 
	 @Autowired
	 private SectionService sections;
	 
	  
	//挂号查询所有或根据id
	  public  JSONArray  selectRegistrFromAll( /*String cliniccardid*/ String clinicname) {
		   return registrmapper.selectRegistrFromAll(/*cliniccardid*/ clinicname);
	   } 
	  
	  public void updatetriagestate( Integer regid, Integer registr_state,String dname) {
		  registrmapper.updatetriagestate(regid, registr_state,dname);
	  }
	  
	    //挂号医疗卡充值判断余额是否充足
		public String   initmoney(String yumoney,Integer regstrtypeid) {
			 //根据挂号类型id查询挂号的价格
			  List<RegistrType> selectAllregtype = regts.selectAllregtype(regstrtypeid, null);
			  Integer rtype=selectAllregtype.get(0).getRegistrtypeId();
			  System.out.println("挂号类型:"+selectAllregtype.get(0).getRegistrtypeName());
			  System.out.println("挂号价格:"+selectAllregtype.get(0).getRegistrtypePrice());
			  //获取余额
			  System.out.println("余额:"+yumoney);
			   //如果是门诊
			  if(Integer.valueOf(rtype)==1) {
				  System.out.println("余额门诊！！！！！！！！！！！");
				  BigDecimal tmoney=new BigDecimal(80).add(selectAllregtype.get(0).getRegistrtypePrice());
				  if(tmoney.compareTo(new BigDecimal(yumoney))==1) {
					  System.out.println("余额门诊22222222");
					    return  "1";
				  }
			  }else{
				  BigDecimal tmoney=new BigDecimal(100).add(selectAllregtype.get(0).getRegistrtypePrice());
				  if(tmoney.compareTo(new BigDecimal(yumoney))==1) {
					    return "2";
				  }
			  }
	           return "3";
		}
		
		//挂号新增---医疗卡支付要新增医疗卡记录
		public void addregistrfrom( String cliniccardid,String sectionname, Integer registrformType,
																Integer registrfrommoneytype,String registrfrom_name,String retypename) {
		        	System.out.println("医疗卡号："+cliniccardid);
		        	//根据医疗卡号查询医疗卡主键id
		        	 List<ClinicCard>  listid = ccs.selectAllbycardid(0,cliniccardid,null,null);
		        	System.out.println("医疗卡id："+listid.get(0).getClinicId());
		        	//科室id
		        	System.out.println("科室："+sectionname);
		        	//通过获取挂号id查到挂号费用
		        	List<RegistrType> allregtype = null ;
		        	//看诊费
		        	List<Section> selectAllsection = sections.selectAllsection(Integer.parseInt(sectionname),null);
		        	BigDecimal lookmoney=null;
		        	
		        	//选择1门诊和0医疗卡--0为充值--1为消费的
		        	if  (registrformType==1 && registrfrommoneytype==0 ){
		        		  allregtype = regts.selectAllregtype(registrformType, retypename);
		        		  /* ①新增医疗记录
		        		   * ②修改医疗卡余额*/
		        		 //支付费用=挂号费用+看诊费用
		        	    BigDecimal rechange_money=selectAllsection.get(0).getSectionPrice().add(allregtype.get(0).getRegistrtypePrice());
                        System.out.println("门诊医疗卡---科室价格："+selectAllsection.get(0).getSectionPrice());
		        	    res.addrechange(listid.get(0).getClinicId(),1,rechange_money);
		        		ccs.updatejianmoney(rechange_money, listid.get(0).getClinicId());
		        		lookmoney=selectAllsection.get(0).getSectionPrice();
		        	}

		        	//选择门诊和1现金
		        	if  (registrformType==1 && registrfrommoneytype==1 ){
		        		  allregtype = regts.selectAllregtype(registrformType,retypename);
		        		   lookmoney=selectAllsection.get(0).getSectionPrice();
		        	}
		        	
		        	//选择急诊2和医疗卡0
		        	if  (registrformType==2 && registrfrommoneytype==0 ){
		        		  allregtype = regts.selectAllregtype(registrformType,retypename);
		        		   lookmoney=selectAllsection.get(0).getSectionPrice().add(new BigDecimal(10));
		        		  /* ①新增医疗记录
		        		   * ②修改医疗卡余额*/
		        		 //支付费用=挂号费用+看诊费用
		        	    BigDecimal rechange_money=selectAllsection.get(0).getSectionPrice().add(allregtype.get(0).getRegistrtypePrice());
		        	    res.addrechange(listid.get(0).getClinicId(),1,rechange_money);
		        		ccs.updatejianmoney(rechange_money, listid.get(0).getClinicId());
		        		lookmoney=selectAllsection.get(0).getSectionPrice().add(new BigDecimal(10));
		        	}

		        	//选择急诊2和1现金
		        	if  (registrformType==2 && registrfrommoneytype==1 ){
		        		  allregtype = regts.selectAllregtype(registrformType,retypename);
		        		   lookmoney=selectAllsection.get(0).getSectionPrice().add(new BigDecimal(10));
		        	}
		        	
		        	System.out.println("付款方式："+registrfrommoneytype);
		        	System.out.println("挂号类型："+registrformType);
		        	System.out.println("门诊挂号类型价格："+allregtype.get(0).getRegistrtypePrice());
		        	System.out.println("看诊费用："+lookmoney);
		        	
		            registrmapper.addregistrfrom(listid.get(0).getClinicId(), registrformType, Integer.valueOf(sectionname), lookmoney, registrfrommoneytype,registrfrom_name,0);	
		}
		
		 public void updatecallstate(Integer regid, Integer registr_call) {
			 registrmapper.updatecallstate(regid, registr_call);
		 }
		
		 //新增交易记录修改余额
		 public void addchargeRechangeMoney(Integer clinic_ids,BigDecimal rechange_money) {
			 //新增交易记录
			 res.addrechange(clinic_ids,1,rechange_money);
			 //修改用户余额
		 	 ccs.updatejianmoney(rechange_money, clinic_ids);
		 }
		
		  // 挂号打印票据
		  public  JSONArray RegistrTicket() {
			 return  registrmapper.RegistrTicket();
		   }
		  
		  public JSONArray renshu(Integer dates) {
			  return registrmapper.renshu(dates);
		  }

		 
		  
}
